package com.vonchenchen.usbmuxd_android.custom;

import android.content.Context;

import com.vonchenchen.usbmuxd_android.custom.testcmd.DemoProtocolCtlCmd;
import com.vonchenchen.usbmuxd_android.custom.testcmd.NetHeader;
import com.vonchenchen.usbmuxd_android.usbmuxd.client.LocalClient;

import static android.R.attr.port;

/**
 * 用户自定义数据交互协议 与usbmuxd协议层分离
 * Created by lidechen on 3/24/17.
 */

public class TestClient extends LocalClient {

    protected NetHeader mRecordCtlHeader;
    private DemoProtocolCtlCmd mDemoProtocolCtlCmd;

    public static final int MAGIC = 0x68;
    public static final int VERSION = 3;
    public static final byte MSG_CMD1 = 3;//cmd1
    public static final byte MSG_CMD2 = 4;//cmd2

    private int mPort;

    public TestClient(Context context, int port, int recvCacheSize, int sendCacheSize) {
        super(context, port, recvCacheSize, sendCacheSize);
        mPort = port;
    }

    public int getPort(){
        return mPort;
    }

    protected void initRecordHeader() {

        mRecordCtlHeader = getNetHeader();
        mDemoProtocolCtlCmd = new DemoProtocolCtlCmd();

        mDemoProtocolCtlCmd.phr = mRecordCtlHeader;
        mDemoProtocolCtlCmd.source = 2;
    }

    protected NetHeader getNetHeader() {

        NetHeader netHeader = new NetHeader();

        netHeader.magic1 = MAGIC;
        netHeader.magic2 = MAGIC;
        netHeader.version = VERSION;

        return netHeader;
    }

    private void sendCMD(byte cmd, String uuid) {

        initRecordHeader();
        mRecordCtlHeader.type = cmd;
        byte[] senddata = mDemoProtocolCtlCmd.getByteArray(uuid);
        send(senddata);
    }

    public void sendCmd1(String mUUid) {
        sendCMD(MSG_CMD1, mUUid);
    }

    public void sendCmd2() {
        sendCMD(MSG_CMD2, "");
    }
}
